package cn.virens.web.components.spring.permission;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.StrUtil;

public class PermissionNode implements Serializable {
	private static final long serialVersionUID = -5236452904006893570L;
	private final List<PermissionNode> childrens = new ArrayList<>();

	private String id;
	private String pid;
	private String name;
	private String perms;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPerms() {
		return perms;
	}

	public void setPerms(String perms) {
		this.perms = perms;
	}

	public boolean isView() {
		return Permission.VIEW.has(perms);
	}

	public boolean isManage() {
		return Permission.MANAGE.has(perms);
	}

	public List<PermissionNode> getChildrens() {
		return childrens.isEmpty() ? null : childrens;
	}

	public boolean addChildren(PermissionNode node) {
		// 尝试添加到下级
		for (PermissionNode children : childrens) {
			if (children.addChildren(node)) { return true; }
		}

		// 添加到当前
		if (StrUtil.equals(node.pid, id)) {
			return childrens.add(node);
		} else {
			return false;
		}
	}

}
